//
//  ZLW_VideoCell.m
//  Otaku
//
//  Created by lanou3g on 16/5/12.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "ZLW_VideoCell.h"

@interface ZLW_VideoCell ()<UIAlertViewDelegate,UMSocialUIDelegate,VideoTableViewCellDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar_url_iamge;
@property (weak, nonatomic) IBOutlet UILabel *Name;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UIButton *categoryName;

@property (weak, nonatomic) IBOutlet UIImageView *videoImageView;
@property (weak, nonatomic) IBOutlet UILabel *playCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UIButton *Good;
@property (weak, nonatomic) IBOutlet UIButton *Bad;
@property (weak, nonatomic) IBOutlet UIButton *comment_count;
@property (weak, nonatomic) IBOutlet UIButton *share_count;
@end

@interface ZLW_VideoCell ()


@end
@implementation ZLW_VideoCell
+(instancetype)cell
{
    
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:self options:nil]lastObject];
}
-(void)awakeFromNib
{
    self.autoresizingMask = NO;
    self.autoresizesSubviews = NO;
    UIImageView *iamgeView = [[UIImageView alloc] init];
    iamgeView.image = [UIImage imageNamed:@"CellBackground"];
    self.backgroundView = iamgeView;
    
}
// 开始播放按钮
- (IBAction)startBtnAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(clickVideoButton:)]) {
        [self.delegate clickVideoButton:self.indexPath];
    }
}
-(void)setGetmodel:(ZLW_VideoModel *)getmodel
{
    if (_getmodel != getmodel) {
        _getmodel = getmodel;
    }
    
    [self.avatar_url_iamge sd_setImageWithURL:[NSURL URLWithString:self.getmodel.avatar_url]];
    self.avatar_url_iamge.layer.cornerRadius = 15;
    self.avatar_url_iamge.layer.masksToBounds = YES;
    self.Name.text = self.getmodel.name;
    self.content.text = self.getmodel.content;
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld",self.getmodel.duration / 60,self.getmodel.duration % 60];
    self.playCountLabel.text = [NSString stringWithFormat:@"%ld 次播放",self.getmodel.play_count];
    
    [self.categoryName setTitle:self.getmodel.category_name forState:(UIControlStateNormal)];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (IBAction)GoodAction:(UIButton *)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"add_one@3x" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
    UIImageView *imgView = [[UIImageView alloc]initWithImage:image];
    imgView.alpha = 1;
    [sender addSubview:imgView];
    
    if (sender.selected) {
        
        [self.Good setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.digg_count+1] forState:(UIControlStateNormal)];
        [UIView animateWithDuration:1 animations:^{
            imgView.alpha = 0;
        } completion:^(BOOL finished) {
            [imgView removeFromSuperview];
            
        }];
        
    }
    else
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"你已经选择" preferredStyle:(UIAlertControllerStyleAlert)];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:nil]];
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
        
    }
    
    
}
- (IBAction)BadAction:(UIButton *)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"add_one_night@3x" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
    UIImageView *imgView = [[UIImageView alloc]initWithImage:image];
    imgView.alpha = 1;
    [sender addSubview:imgView];
    
    if (sender.selected) {
        
        [UIView animateWithDuration:1 animations:^{
            imgView.alpha = 0;
        } completion:^(BOOL finished) {
            [imgView removeFromSuperview];
            [self.Bad setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.bury_count+1] forState:(UIControlStateNormal)];
            sender.enabled = NO;
        }];
    }else
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"你已经选择" preferredStyle:(UIAlertControllerStyleAlert)];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:nil]];
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
    }
}

- (IBAction)category:(UIButton *)sender {
}

- (IBAction)share_url:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:self.getmodel.share_url];
    [[UIApplication sharedApplication] openURL:url];
}
- (IBAction)share:(UIButton *)sender {
    
    [UMSocialSnsService presentSnsIconSheetView:[UIApplication sharedApplication].keyWindow.rootViewController appKey:@"572c6a8ce0f55ac72d000352" shareText:self.getmodel.content
                                     shareImage:[UIImage imageNamed:@"icon"]shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToQzone,UMShareToEmail,UMShareToSms,nil]delegate:self];
    
    [UMSocialData defaultData].extConfig.title = self.Name.text;
    [UMSocialData defaultData].shareText = [NSString stringWithFormat:@"%@+%@",self.Name.text,self.share_count.titleLabel.text];
    
    
}
-(void)layoutSubviews
{
    
#pragma mark  点赞数目
    [self.Good setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.digg_count] forState:(UIControlStateNormal)];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"digupicon_comment_press@3x" ofType:@"png"];
    UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:path1];
    [self.Good setImage:image1  forState:(UIControlStateNormal)];
    
    
    
    
#pragma mark    不好数目
    [self.Bad setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.bury_count] forState:(UIControlStateNormal)];
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"digdownicon_textpage_press@3x" ofType:@"png"];
    UIImage *image2 = [[UIImage alloc] initWithContentsOfFile:path2];
    [self.Bad setImage:image2  forState:(UIControlStateNormal)];
#pragma mark 评论数目
    [self.comment_count setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.comment_count] forState:(UIControlStateNormal)];
    NSString *path3 = [[NSBundle mainBundle] pathForResource:@"commenticon_textpage_night@3x" ofType:@"png"];
    UIImage *image3 = [[UIImage alloc] initWithContentsOfFile:path3];
    [self.comment_count setImage:image3  forState:(UIControlStateNormal)];
# pragma amrk  分享数目
    [self.share_count setTitle:[NSString stringWithFormat:@"%ld",(long)self.getmodel.share_count] forState:(UIControlStateNormal)];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"html_share_night@3x" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
    [self.share_count setImage:image  forState:(UIControlStateNormal)];
    
    
    
    
    
}

+ (CGFloat)heightModel:(ZLW_VideoModel *)model
{
    CGFloat heightContent = [Tool labelHeightWithText:model.content font:[UIFont systemFontOfSize:17]];
    return model.cellHeight + heightContent + 150 ;
}





@end
